package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.PrInvoiceDTO;
import com.financing.wap.service.IPrInvoiceService;

@Controller("prInvoiceController")
@RequestMapping("/api/prInvoice")
public class PrInvoiceController extends BaseController {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IPrInvoiceService prInvoiceService;
	
	@RequestMapping("/listPrInvoice")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, PrInvoiceDTO prInvoice,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query prInvoice list begin .......");
			
			prInvoice.setBegin(pager.getStartIndex());
			prInvoice.setPageSize(pager.getPageSize());
			
			int count = prInvoiceService.queryListCount(prInvoice);
			
			pager.setTotalCount(count);
			
			List<PrInvoiceDTO> prInvoiceList = null;
			
			if(count > 0){
				prInvoiceList = prInvoiceService.queryList(prInvoice);
			}else{
				prInvoiceList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", prInvoiceList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query prInvoice list end .......");
		}catch(Exception e){
			log.error("query prInvoice error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addPrInvoice")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, PrInvoiceDTO prInvoice){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add prInvoice  begin .......");
			
			DTOdateSetUtil.setCreateInfo(prInvoice);
			DTOdateSetUtil.setUpdateInfo(prInvoice);
			
			int id = prInvoiceService.insert(prInvoice);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add prInvoice list end .......");
		}catch(Exception e){
			log.error("add prInvoice error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailPrInvoice")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, PrInvoiceDTO prInvoice){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail prInvoice  begin .......");
			
			Integer id = prInvoice.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			PrInvoiceDTO dto = prInvoiceService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail prInvoice  end .......");
		}catch(Exception e){
			log.error("query detail prInvoice error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deletePrInvoice")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, PrInvoiceDTO prInvoice){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete prInvoice  begin .......");
			
			Integer id = prInvoice.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			prInvoiceService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete prInvoice  end .......");
		}catch(Exception e){
			log.error("delete prInvoice error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updatePrInvoice")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, PrInvoiceDTO prInvoice){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update prInvoice  begin .......");
			
			Integer id = prInvoice.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prInvoice);
			
			prInvoiceService.update(prInvoice);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update prInvoice list end .......");
		}catch(Exception e){
			log.error("update prInvoice error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForcePrInvoice")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, PrInvoiceDTO prInvoice){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce prInvoice  begin .......");
			
			Integer id = prInvoice.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(prInvoice);
			
			prInvoiceService.updateForce(prInvoice);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce prInvoice list end .......");
		}catch(Exception e){
			log.error("updateForce prInvoice error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
